<?php
namespace App\Logic;


use App\Models\SwtBjMachine;
use App\Models\SwtMachine;

class MachineLogic extends BaseLogic {

    public static function addMachine($info)
    {
        $machine_info = SwtMachine::firstWhere('machine_no', $info['machine_no']);

        if($machine_info) {
            self::$error = "设备已存在";
            return false;
        }

        $res = SwtMachine::create($info);

        if(!$res) {
            self::$error = "设备新增失败";
            return false;
        }

        return $res -> id;
    }

    public static function updateTuoYuanMachineRegister($imei, $status)
    {
        $info = SwtBjMachine::firstWhere('imei', $imei);

        if(!$info) {
            self::$error = "设备未找到";
            return false;
        }

        switch($status)
        {
            case 1000 :
                $is_register = 0;
                break;
            case 2000 :
                $is_register = 1;
                break;
            case 3000 :
                $is_register = 3;
                break;
            case 4000 :
                $is_register = 2;
                break;
            default :
                $is_register = 4;
                break;
        }

        $info -> is_register = $is_register;

        $res = $info -> save();

        if(!$res) {
            self::$error = "设备状态更新失败";
            return false;
        }

        return true;
    }
}
